package com.shopAuth.controller;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shopAuth.service.ICheckAuthService;
import com.shopUser.feign.UserFeign;

import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.PhoneFormatCheckUtils;
import shopBean.common.utils.RestResultGenerator;

@Controller
@RequestMapping("check")
@SuppressWarnings("rawtypes")
public class CheckAuthController {
	@Autowired
	ICheckAuthService checkAuthService;
	@Autowired
	UserFeign userFeign;
	@Autowired
	RedisTemplate redisTemplate;
	@Value("${spring.profiles.active}")
	String systemRun;

	/**
	 * 发送验证码
	 * 
	 * @param tel
	 *            手机号
	 * @return 返回成功状态
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("/sendCode.do")
	public RestResult sendCode(String tel, Integer type) throws Exception {
		if (StringUtils.isEmpty(tel)) {
			return RestResultGenerator.genErrorResult("手机号不为空", null, Status.tel_is_empty);
		}
		if (null == type) {
			return RestResultGenerator.genErrorResult("类型标识为空", null, Status.type_empty);
		}

		if (!PhoneFormatCheckUtils.isPhoneLegal(tel)) {
			return RestResultGenerator.genErrorResult("手机号格式不正确", null, Status.tel_invalid);
		}
		String redisKey = "SMS_LIMIT_" + tel;
		try {
			long count = redisTemplate.opsForValue().increment(redisKey, 1);
			if (count == 1) {
				// 设置有效期一分钟
				redisTemplate.expire(redisKey, 60, TimeUnit.SECONDS);
			}
			if (count > 1) {
				return RestResultGenerator.genErrorResult("短信发送频繁，请稍后再试", null, Status.msg_frequently_error);
			}
			
			RestResult restResult = userFeign.getByAccountApi(tel);
			if (restResult == null || !restResult.getStatusCode().equals(Status.sys_success)) {
				return RestResultGenerator.genErrorResult("用户服务不可用,请联系管理员", null, Status.sys_error);
			}
			// 发送手机注册验证码
			if (type == 1) {

				if (restResult.getData() != null) {
					return RestResultGenerator.genErrorResult("手机号已存在", null, Status.user_is_exits);
				}
			} else {
				// 找回手机号密码
				if (restResult.getData() == null) {
					return RestResultGenerator.genErrorResult("手机号不存在", null, Status.user_is_no_exits);
				}
			}
		} catch (Exception e) {
			throw e;
		} finally {
			redisTemplate.delete(redisKey);
		}
		return checkAuthService.sendCode(tel);
	}

	/**
	 * 用户是否注册过
	 * 
	 * @param tel
	 *            手机号
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/userCheck.do")
	public RestResult userCheck(String tel) {
		RestResult restResult = userFeign.getByAccountApi(tel);
		if (restResult == null || !restResult.getStatusCode().equals(Status.sys_success)) {
			return RestResultGenerator.genErrorResult("用户服务不可用,请联系管理员", null, Status.sys_error);
		}
		if (restResult.getData() == null) {
			return RestResultGenerator.genSuccessResult(true);
		} else {
			return RestResultGenerator.genSuccessResult(false);
		}
		
	}

	@ResponseBody
	@RequestMapping("/checkCodeOk.do")
	public RestResult userCheck(String tel, String code) {

		if (systemRun.equals("dev")) {
			return RestResultGenerator.genSuccessResult(true);
		} else {
			return RestResultGenerator.genSuccessResult(checkAuthService.checkCode(tel, code, false));
		}
	}
	@RequestMapping("/checkCode.api")
	@ResponseBody
	public RestResult checkCode(String tel,String code,Boolean flag) {
		if(StringUtils.isBlank(tel)) {
			return RestResultGenerator.genErrorResult("手机号不能为空", null, Status.tel_is_empty);
		}
		if(StringUtils.isBlank(code)) {
			return RestResultGenerator.genErrorResult("验证码不能为空", null, Status.code_is_empty);
		}
		if(flag == null) {
			flag = false;
		}
		return RestResultGenerator.genSuccessResult(this.checkAuthService.checkCode(tel, code, flag));
	}
	
}
